<template>
  <div class="dialog-container">
    <el-form ref="routeManHourRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="" prop="workHour">
        <el-input v-model="form.workHour" placeholder="请输入每日标准工作时长">
          <template #prepend>
            司机每日标准工作时长
          </template>
          <template #append>
            小时
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="" prop="timingSpeed">
        <el-input v-model="form.timingSpeed" placeholder="">
          <template #prepend>
            驾驶员驾驶车辆车速须大于等于
          </template>
          <template #append>
            km/h时才记为运行时间；
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="" prop="timingEndDuration">
        <el-input v-model="form.timingEndDuration" placeholder="">
          <template #prepend>
            驾驶员驾驶车辆车速为0后大于等于
          </template>
          <template #append>
            分钟停止运行时间的计算
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status">
          <el-option v-for="dict in sys_data_status" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <div class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="$emit('cancel')">取 消</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup name="RouteManHour">
import { getRouteManHour, updateRouteManHour } from "@/api/dispatch/routeManHour";

const { proxy } = getCurrentInstance();
const { sys_data_status } = proxy.useDict('sys_data_status');

const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const title = ref("");

const data = reactive({
  form: {},
  rules: {
    workHour: [
      { required: true, message: "注：如果线路没有规定工作时长请输入0", trigger: "blur" }
    ],
    timingSpeed: [
      { required: true, message: "每日标准工作时长不能为空", trigger: "blur" }
    ],
    timingEndDuration: [
      { required: true, message: "注：如果须运行时间不收规则限制请设置计算车速为0即可", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

const props = defineProps({
  // 当前的值
  routeId: [Number, String]
});
const emit = defineEmits(['cancel'])

// 表单重置
function reset() {
  form.value = {
    routeId: props.routeId,
    workHour: null,
    timingSpeed: null,
    timingEndDuration: null,
    status: '0'
  };
  proxy.resetForm("routeManHourRef");
}


/** 修改按钮操作 */
function handleUpdate(routeId) {
  loading.value = true
  reset();
  getRouteManHour(routeId).then(response => {
    loading.value = false;
    if (response.data)
      form.value = response.data;
    open.value = true;
    title.value = "修改线路工时配置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["routeManHourRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      updateRouteManHour(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        emit('cancel')
      }).finally(() => {
        buttonLoading.value = false;
      });

    }
  });
}


watch(props, (newVal, oldVal) => {
  handleUpdate(newVal.routeId)
}, { immediate: true });
</script>
